package com.lazy.book.service.system.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysMessageUserMapper;
import com.lazy.book.pojo.system.pojo.po.SysMessageUser;
import com.lazy.book.service.system.SysMessageUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysMessageUserServiceImpl extends ServiceImpl<SysMessageUserMapper, SysMessageUser> implements SysMessageUserService {

    private final SysMessageUserMapper sysMessageUserMapper;

    @Override
    public void batchInsert(Long messageId, List<Object> receiverIds) {
        List<SysMessageUser> list = receiverIds.stream().map(receiverId -> {
            SysMessageUser sysMessageUser = new SysMessageUser();
            sysMessageUser.setMessageId(messageId);
            sysMessageUser.setUserId(Long.valueOf(receiverId.toString()));
            return sysMessageUser;
        }).collect(Collectors.toList());
        sysMessageUserMapper.insertBatch(list);
    }

    @Override
    public String getIsRead(Long messageId, Long receiverId) {
        SysMessageUser sysMessageUser = getOne(QueryWrapper.create().eq(SysMessageUser::getMessageId, messageId).eq(SysMessageUser::getReceiverId, receiverId));
        if (sysMessageUser == null) {
            return "F";
        }
        return sysMessageUser.getIsRead();
    }

}
